import {chooseCard} from '../../../assets/js/toonModule.js'
import util from '../../../assets/js/util'
import Toast from 'toon-ui/lib/components/toast'
export default {
  data() {
    return {
      detailData: {
        "content": "", //内容
        "photos": [],  // 图片
        "createDate": "", // 创建时间
        "praised": true,  // 是否被赞过
        "praiseCount": 0,  // 赞数
        "commentCount": 0 // 评论数
      },
      commentList: [], // 评论列表
      curPage: 0,
      pageNum: '1',  //当前页
      pageSize: '10', // 每页个数
      isShowContent: false, // 显示详情状态,
      alert: false // 输入框弹起
    }
  },
  filters: {
    thumbnail: function (url, param) {
      return url.replace(".png", param + ".png").replace(".jpg", param + ".jpg")
    },
    "changeTimeYMD": function (value) {
      return value;
      //return util.dateFormat('yyyy-MM-dd', value)
    },
    "changeTimeHM": function (value) {
      return value;
      //return util.dateFormat('hh:mm', value)
    },
    /**
   * 0 => 赞
   */
    changeVote: function (num) {
      return num > 0 ? num : '赞'
    },
    /**
    * 0 => 评论
    */
    changeDis: function (num) {
      return num > 0 ? num : '评论'
    },
  },
  mounted() {
    let _this = this;
    this.init();
    this.$util.setTitle('大赞门城');

  },
  methods: {
    init() {
      // 初始化
      this.curPage = 0;
      this.pageNum = 1; //当前页
      this.pageSize = 10; // 每页个数
      this.content = ''; // 清除评论
      this.ShowContent = false;
      this.commentList = [];
      // 获取详情
      this.getDetailById();

    },
    /**
     * 获取详情
     */
    getDetailById() {
      let _this = this;
      let id = _this.$util.getQueryStringHr('id');
      let params = {
        "id": id
      }
      this.$http.post('/praisingCity/getDetailById', params).then(function (rep) {
        console.log(rep);
        if (rep.meta && rep.meta.code == '0') {
          // 详情数据的替换
          _this.detailData = rep.data;
          _this.detailData.content = _this.detailData.content.replace(/\n/g, "<br>");

          // 显示详情
          _this.isShowContent = true;
          // 获取评论列表
          _this.getCommentListById();
          setTimeout(function () {
            _this.$util.droploadUpOnScroll(_this.getCommentListById, 'more', 'mainScroll')
          }, 0);
        }
      }).catch(function (err) {

      })
    },
    /**
     * 获取评论列表
     */
    getCommentListById() {
      let _this = this;
      let id = _this.$util.getQueryStringHr('id');
      if (_this.curPage == _this.pageNum) {
        return
      } else {
        _this.curPage = _this.pageNum
      }

      let params = {
        "id": id,
        "pageNum": _this.pageNum,
        "pageSize": _this.pageSize
      }
      _this.$http.post('/praisingCity/queryCommentListById', params).then(function (rep) {
        if (rep.meta && rep.meta.code == '0') {
          if (rep.data.length > 0) {
            _this.commentList = _this.commentList.concat(rep.data);
          }
          if (rep.data.length == _this.pageSize) {
            _this.pageNum++;
          }
        }
      }).catch(function (err) {
        console.log(err)
      })
    },
    /**
     * 点赞
     */
    praise() {
      if (!this.toonNETWORK()) { return }
      let _this = this;
      let id = _this.$util.getQueryStringHr('id');
      let params = {
        'id': id
      }
      this.$http.post('/praisingCity/praise', params).then(function (rep) {
        _this.detailData.praised = !_this.detailData.praised;
        console.log(_this.detailData.praised)
        if (_this.detailData.praised) {
          _this.detailData.praiseCount = _this.detailData.praiseCount - 0 + 1;
        } else {
          _this.detailData.praiseCount = _this.detailData.praiseCount - 0 - 1;
        }
      }).catch(function (err) {
        console.log(err)
      })
    },
    /**
     * 检查网络
     */
    toonNETWORK() {
      if (navigator.onLine) {
        return true
      }
      Toast({
        message: '无网络连接，请稍后重试',
        duration: 1000, // 持续时间，默认3000毫秒
      });
      return false
    },
    /**
     * 提交评论
     */
    addComment() {
      if (!this.toonNETWORK()) { return }
      let _this = this;
      let id = _this.$util.getQueryStringHr('id');
      let content = _this.content;
      // 内容为空
      if (content == '' || !content) {
        Toast({
          'message': '评论内容不能为空',
          'duration': 2000
        })
      } else {
        _this.choose(function (userInfo) {
          // 如果点击取消
          if (userInfo.data.result == 'cancel') {

          } else {
            let params = {
              "commentAvator": userInfo.data.avatarId,
              "commentName": userInfo.data.title,
              "content": content,
              "feedId": userInfo.data.feedId,
              "id": id
            }
            _this.$http.post('/praisingCity/addComment', params).then(function (rep) {
              if (rep.meta && rep.meta.code == '0') {
                _this.init();
              }
            }).catch(function (err) {

            })

          }

        });
      }
    },
    choose(callback) {
      let _this = this;
      chooseCard(function (data) {
        callback(data);
      })
    },
    textFoucs(bool) {
      this.alert = bool;
    }

  }
}
